/**
 * zmn渠道管理-押金管理
 * 文件命名格式：zmn.模块.表/功能.js
 * @author xiangjunming
 * @since 2019/08/22 18:06
 */
layui.define(['form', 'layedit', 'laydate', 'table', 'laytpl'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var laydate = layui.laydate;
    let table = layui.table;
    form.render(null, "zmn-form");

    $(".qrCodeImage1,.qrCodeImage2").hide();
    $(".imagDiv1,.imagDiv2").show();
    $(".imagDiv1,.imagDiv2").parent().css({'padding': "65px"});

    $("#imagDiv1,#imagDiv2").css("background", "url(/static/lay/layui/css/modules/layer/default/loading-2.gif)" +
        " no-repeat");
    getWeChatQrCode(1);

    $("button.layui-btn").on({
        mouseover: function () {
            $(this).removeClass("layui-btn-primary");
            if ($(this).attr("disabled")) {
                $(this).css("border", "1px solid #C9C9C9");
            } else {
                $(this).css("border", "1px solid #009688")
            }
        },
        mouseout: function () {
            $(this).addClass("layui-btn-primary");
            $(this).css("border", "1px solid #C9C9C9");
        },
        click: function () {
            let url;
            if ("1" === this.value) {
                url = $("#imageQrcode1").val();
            } else {
                url = $("#imageQrcode2").val();
            }
            if (!url) {
                return layer.msg("渠道未配置数据，不能下载", {"icon": 2});
            }

            let downLoadUrl = '/channel/downloadQrCode.action';
            let exportForm = $("<form action='" + downLoadUrl + "' method='post'><input name='url' value='" + url + "'></form>")
            $(document.body).append(exportForm);
            exportForm.submit();
            exportForm.remove();
        }
    });

    function showQrCode(data, type) {
        if (data) {
            let url = data.url;
            $("#imageQrcode" + type).val(url);
            // 根据微信二维码URL生成二维码图片显示
            $("#image" + type)[0].src = "/channel/qrCode.action?url=" + url;
            $("#image" + type).show();
            $("#image" + type).parent().css({'padding': "15px"});
            $("#image" + type).parent().css({'padding': "15px"});
            layui.$("#imagDiv" + type).removeAttr("style").hide();
            $(".text-p" + type).hide();
        }
    }

    /**
     * 根据微信二维码URL获取二维码图片
     * @param type
     */
    function getWeChatQrCode(type) {
        let channelId = $("#channelId").val();
        let plat = $("#plat").val();
        // 查询微信永久二维码URL, 如存在直接返回, 不存在生成新的
        let url = json.ZMN_URL_ENV + 'gateway-api.xiujiadian.com/saas/sapi/qrcode/getMultiplePermanentQRCodeByChannelIdAndPlatId';
        let param = {
            "body": {
                "channelId": channelId,
                "platId": plat
            }
        }

        $.ajax({
            // async: false,
            contentType: "application/json;charset=UTF-8",
            type: "POST",
            url: url,
            dataType: "JSON",
            data: JSON.stringify(param),
            xhrFields: {withCredentials: true}, //设置请求可以携带cookie
            crossDomain: true, success: function (resp) {
                if (resp.status == 200) {
                let data = resp.data;
                showQrCode(data.zmn_wechat_account, 1);
                showQrCode(data.zmn_family_repair_account, 2);
                } else {
                layer.msg(resp.message);
                }
            },
            error: function (XMLHttpRequest, status, errorThrown) {
            }
        });
    }

    //对外暴露的接口
    exports('channel_promocode', {});
});